#include "tls_rsa.h"

int pkcs1pad_sign(unsigned char *key, PACKET *in, int in_size, PACKET *out, int out_size) {
	struct crypto_sig *sig;
	int ret;
	sig = crypto_alloc_sig("pkcs1pad(rsa,sha256)", 0, 0);

    if (IS_ERR(sig)) {
        ret = PTR_ERR(sig);
        return -1;
    }
    if (key != NULL) {
        ret = crypto_sig_set_privkey(sig, key + 26, *(short *)(key + 24));
    } else {
        printk("pkcs1pad_sign: key is NULL!\n");
    }
	// printk("crypto_sig_set_privkey ret:%d\n", ret);
	ret = crypto_sig_sign(sig, in, in_size, out, out_size);
	printk("crypto_sig_sign ret:%d\n", ret);
	return ret;
}

int pkcs1pad_verify(unsigned char *key, PACKET *in, int in_size, PACKET *out, int out_size) {
	struct crypto_sig *sig;
	int ret;
	sig = crypto_alloc_sig("pkcs1pad(rsa,sha256)", 0, 0);

    if (IS_ERR(sig)) {
        ret = PTR_ERR(sig);
        return -1;
    }
    ret = crypto_sig_set_pubkey(sig, key, 526);
	// printk("crypto_sig_set_pubkey ret:%d\n", ret);
	ret = crypto_sig_verify(sig, out, 512, in, 32);
	printk("crypto_sig_verify ret:%d\n", ret);
	return ret;
}
